$(function () {
    var user = null;
    var filingPage = function () {
        this.init();
    };

    filingPage.prototype = {

        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {

            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user || !user.id) {
                redirect("../account/login.html");
                return false;
            }

            //初始化区域
            this.loadRegion();
        },
        bindEvent: function () {
            //点击回退
            $("#back").on("tap", function () {
                back();
            });
            //滚动到底部时显示向上箭头
            $(window).scroll([], this.isDisplayTopIcon);
            //点击回到顶部
            $("#gotop").on("tap", function () {
                $('html,body').animate({scrollTop: '0px'}, 500);
            });
            //点击			
            $("#creater,#name,#supplier_industry,#address,#phone,#chargeman_name,#linkman_name,#linkman_phone").on("input", function () {
                thisPage.bindCommitEvent();
            });
            $("#country").on("change", function () {
                thisPage.bindCommitEvent();
            });
            $("input[name='supplier_from'],input[name='supplier_visit_type']").on("click", function () {
                thisPage.bindCommitEvent();
            });
        },
        bindCommitEvent: function () {
            if ("" == $("#creater").val() ||
                "" == $("#name").val() ||
                "" == $("#supplier_industry").val() ||
                "0" == $("#country").val() ||
                "" == $("#address").val() ||
                "" == $("#phone").val() ||
                "" == $("#chargeman_name").val() ||
                "" == $("#linkman_name").val() ||
                "" == $("#linkman_phone").val() ||
                0 == $("input[name='supplier_from']:checked").length ||
                0 == $("input[name='supplier_visit_type']:checked").length
            ) {
                $("#commit").addClass("gray_color").unbind();
            } else {
                $("#commit").removeClass("gray_color").unbind().on("tap", function () {
                    thisPage.commitEvent();
                });
            }
        },
        commitEvent: function () {
            $("#commit").on("tap", function () {
                if (!isChineseName($("#creater").val())) {
                    $.alert("请填写正确的申请人姓名");
                    return false;
                }
                if (!isChineseCharacter($("#name").val(), 5, 20)) {
                    $.alert("请填写正确的公司名称");
                    return false;
                }
                if (!isChineseCharacter($("#supplier_industry").val(), 2, 10)) {
                    $.alert("请选择所属行业");
                    return false;
                }
                if ("0" == $("#country").val()) {
                    $.alert("请选择所属区域");
                    return false;
                }
                if ($("#address").val().length < 10 || $("#address").val().length > 50) {
                    $.alert("填写正确的详细地址");
                    return false;
                }
                if (!isPhone($("#phone").val())) {
                    $.alert("填写正确的公司电话");
                    return false;
                }
                if (!isChineseName($("#chargeman_name").val())) {
                    $.alert("填写正确的公司负责人");
                    return false;
                }
                if (!isChineseName($("#linkman_name").val())) {
                    $.alert("填写正确的公司联系人");
                    return false;
                }
                if (!isPhone($("#linkman_phone").val())) {
                    $.alert("填写正确的联系电话");
                    return false;
                }
                var requestData = {
                    creater: $("#creater").val(),
                    name: $("#name").val(),
                    supplier_industry: $("#supplier_industry").val(),
                    region_no: $("#country").val(),
                    address: $("#address").val(),
                    phone: $("#phone").val(),
                    chargeman_name: $("#chargeman_name").val(),
                    linkman_name: $("#linkman_name").val(),
                    linkman_phone: $("#linkman_phone").val(),
                    supplier_from: $("input[name='supplier_from']:checked").val(),
                    supplier_visit_type: $("input[name='supplier_visit_type']:checked").val(),
                };
                $.ajax({
                    type: "POST",
                    url: CONFIG_MANAGER.RequstUrl + "v1/settled/supplierpply",
                    async: true,
                    contentType: 'application/json',
                    headers: {token: user.token}
                }).done(function (data) {
                    $.alert("提交成功", function () {
                        redirect("../finance/income.html");
                        return false;
                    });
                }).fail(function (data) {
                    var infor = JSON.parse(data);
                    $.alert(infor.Message);
                });
            });
        },
        //初始化区域
        loadRegion: function () {
            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/basic/region/child/CN",
                async: true
            }).done(function (data) {
                var i = 0, html = ["<option value='0' selected='selected'>选择省</option>"];
                while (data[i]) {
                    html.push("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                    i++;
                }
                $("#province").html(html.join(''));
                //选择省
                $("#province,#city").on("change", function () {
                    var value = $(this).val(), id = $(this).attr("id");
                    $.ajax({
                        type: "GET",
                        url: CONFIG_MANAGER.RequstUrl + "v1/basic/region/child/" + value,
                        async: true
                    }).done(function (data) {
                        var i = 0, html = [];
                        while (data[i]) {
                            html.push("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                            i++;
                        }
                        if ("province" == id) {
                            html.unshift("<option value='0' selected='selected'>选择市</option>");
                            $("#city").html(html.join(''));
                        } else {
                            html.unshift("<option value='0' selected='selected'>选择区</option>");
                            $("#country").html(html.join(''));
                        }
                    });

                });

            });

        },
        isDisplayTopIcon: function () {
            var windowHeight = $(window).height();		//可是区域高度
            var scrollTop = $(window).scrollTop();		//滚动条在Y轴滚动距离
            var scrollHeight = $(document).height();	//内容可视区域+溢出（滚动）
            if ((scrollTop + windowHeight) == scrollHeight) {
                $("#gotop").show();
            } else {
                $("#gotop").hide();
            }
        }
    };

    var thisPage = new filingPage();

});
